package leetcode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LeetCode1 {

    public int[] twoSum1(int[] nums, int target) {
        Map<Integer, List<Integer>> dict = new HashMap<>();
        List<Integer> index;
        for (int i = 0; i < nums.length; i++) {
            dict.computeIfAbsent(nums[i], k -> new ArrayList<>());
            dict.get(nums[i]).add(i);
        }
        for (int i = 0; i < nums.length; i++) {
            if ((index = dict.get(target - nums[i])) != null) {
                for (Integer integer : index) {
                    if (integer != i)
                        return new int[]{i, integer};
                }
            }
        }
        return null;
    }

    //1
    public int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> dict = new HashMap<>();
        Integer index;
        for (int i = 0; i < nums.length; i++) {
            if ((index = dict.get(target - nums[i])) != null)
                return new int[]{i, index};
            dict.put(nums[i], i);
        }
        return null;
    }
}
